package net.simo.servicebook.service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import net.simo.servicebook.model.Color;
import net.simo.servicebook.model.Maintenance;
import net.simo.servicebook.model.Person;
import net.simo.servicebook.model.Vehicle;
import net.simo.servicebook.model.VehicleMake;
import net.simo.servicebook.model.VehicleModel;
import net.simo.servicebook.model.VehicleModification;
import net.simo.servicebook.model.security.User;

@Stateless
public class VehicleManager implements Serializable {

	private static final long serialVersionUID = -3418318821915193806L;

	@Inject
	private EntityManager em;

	private Date date;

	public List<Vehicle> userVehicle(User user) {
		String hql = "FROM Vehicle WHERE person_id = :personId ";

		TypedQuery<Vehicle> query = em.createQuery(hql, Vehicle.class);
		query.setParameter("personId", user.getPerson().getPersonId());
		List<Vehicle> result = query.getResultList();
		return result;
	}

	public List<VehicleMake> getMakes() {
		String hql = "FROM VehicleMake ";

		TypedQuery<VehicleMake> query = em.createQuery(hql, VehicleMake.class);
		List<VehicleMake> result = query.getResultList();
		return result;
	}

	public List<VehicleModel> getModels(VehicleMake make) {
		String hql = "FROM VehicleModel WHERE vehicle_make_id = :vehicleMakeId ";

		TypedQuery<VehicleModel> query = em.createQuery(hql, VehicleModel.class);
		query.setParameter("vehicleMakeId", make.getVehicleMakeId());
		List<VehicleModel> result = query.getResultList();
		return result;
	}

	public List<VehicleModification> getModifications(VehicleModel model) {
		String hql = "FROM VehicleModification WHERE vehicle_model_id = :vehicleModelId ";

		TypedQuery<VehicleModification> query = em.createQuery(hql, VehicleModification.class);
		query.setParameter("vehicleModelId", model.getVehicleModelId());
		List<VehicleModification> result = query.getResultList();
		return result;
	}

	public List<Color> getColors() {
		String hql = "FROM Color";

		TypedQuery<Color> query = em.createQuery(hql, Color.class);
		List<Color> result = query.getResultList();
		return result;
	}

	public void saveVehicle(Vehicle vehicle) {
		VehicleModel model = em.find(VehicleModel.class, vehicle.getVehicleModel().getVehicleModelId());
		Color color = em.find(Color.class, vehicle.getColor().getColorId());
		VehicleModification modification = em.find(VehicleModification.class, vehicle.getVehicleModification().getVehicleModificationId());
		vehicle.setVehicleModel(model);
		vehicle.setVehicleModification(modification);
		vehicle.setColor(color);
		em.persist(vehicle);
	}

	public List<Maintenance> getMaintenance(Vehicle vehicle) {
		String hql = "FROM Maintenance WHERE vehicle_id = :vehicleId ";

		TypedQuery<Maintenance> query = em.createQuery(hql, Maintenance.class);
		query.setParameter("vehicleId", vehicle.getVehicleId());
		List<Maintenance> result = query.getResultList();
		return result;
	}

	public void saveMaintenance(Maintenance maintenance) {
		date = new Date();
		maintenance.setDate(date);
		em.persist(maintenance);
	}

	public void deleteMaintenance(Maintenance maintenance) {
		maintenance = em.find(Maintenance.class, maintenance.getMaintenanceId());
		em.remove(maintenance);
	}

	public void editMaintenance(Maintenance maintenance) {
		em.merge(maintenance);
	}
	
	public List<Vehicle> personVehicle(Person person) {
		String hql = "FROM Vehicle WHERE person_id = :personId";
		
		TypedQuery<Vehicle> query = em.createQuery(hql, Vehicle.class);
		query.setParameter("personId", person.getPersonId());
		List<Vehicle> result = query.getResultList();
		return result;
	}

}
